<template>
  <div id="app">
    <div class="box">
      <h3>计算属性</h3>
      <div>
        <p>Original message: "{{ message }}"</p>
        <p>Original msg: "{{ msg }}"</p>
        <p>Computed reversed message: "{{ reversedMessage }}"</p>
        <button @click="change">修改 message</button>
      </div>
    </div>

    <div class="box">
      <h3>侦听器</h3>
      <div>
        <p>Original twatch: "{{ twatch }}"</p>
        <p>rwatch value: " {{ rwatch }}"</p>
        <button @click="change1">修改 twatch</button>
      </div>
    </div>

    <div class="box">
      <h3>计算属性 setter</h3>
      <div>
        <p>first name: {{firstName}}</p>
        <p>last name: {{lastName}}</p>
        <p class="tip">fullname： {{ fullName}}</p>
        <button @click="change2">修改fullname为宋人</button>
      </div>
    </div>

    <div class="box">
      <h3>axios</h3>
      <div>
        <!-- 生命周期函数， mounted -->
        <span class="tip">请求的数据：</span>
        <span>{{axiosText}}</span>
      </div>
    </div>

    <div class="box">
      <span class="tip">{{articleTitle}}</span>
      <CItem  :title="articleTitle"  @changeTitle="onChange" />

    </div>


  </div>
</template>

<script>
import CItem from "./components/CItem"

export default {
  name: "App",
  components: {
    CItem
  },
  data: function() {
    return {
      message: "Hello",
      msg: "world",
      twatch: "watch 变量",
      rwatch: "empty",
      firstName: "tang",
      lastName: "chao",
      axiosText: "",
      articleTitle: "今日新闻"
    };
  },
  mounted: function() {
    this.$axios.get("data.json")
        .then(res => {
          console.log("=============================================")
          console.log(res)
          this.axiosText = res.author
          console.log("=============================================")
        })
  },
  methods: {
    change: function() {
      this.message = "world";
    },
    change1: function() {
      this.twatch = "changed twached";
    },
    change2: function () {
      this.fullName = "song ren"
    },
    onChange: function (val) {
      this.articleTitle = val
    }
  },
  computed: {
    // 计算属性的 getter
    reversedMessage: function() {
      // `this` 指向 vm 实例   123   321
      let resMessage = this.message
        .split("")
        .reverse()
        .join("");
      let resMsg = this.msg
        .split("")
        .reverse()
        .join("");
      return resMessage + resMsg;
    },
    fullName: {
      // getter
      get: function() {
        return this.firstName + " " + this.lastName;
      },
      // setter
      set: function(newValue) {
        var names = newValue.split(" ");
        this.firstName = names[0];
        this.lastName = names[names.length - 1];
      }
    }
  },
  watch: {
    twatch: function(newValue, oldValue) {
      this.rwatch = "newValue:" + newValue + ";oldValue:" + oldValue;
    }
  }
};
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
.box {
  border-bottom: 1px solid red;
  margin-bottom: 20px;
  padding-bottom: 5px;
}
.tip {
  color: red;
}
</style>
